package mongodb

type Creator struct {
	AppName        string     `json:"appName"`
	Table          *Table     `json:"table"`
	Fields         []*Field   `json:"fields"`
	RelationTables []*Creator `json:"relationTables"`
}

func NewDocument(name string, cnName string, path string) *Creator {
	c := new(Creator)
	t := new(Table)
	t.Name = name
	t.Path = path
	t.CnName = cnName
	c.Table = t
	c.Fields = make([]*Field, 0)
	c.RelationTables = make([]*Creator, 0)
	return c
}

func (c *Creator) GetTable() *Table {
	return c.Table
}
func (c *Creator) SetName(name string) *Table {
	c.Table.Name = name
	return c.Table
}

func (c *Creator) SetCnName(cnName string) *Table {
	c.Table.CnName = cnName
	return c.Table
}

func (c *Creator) SetPath(path string) *Table {
	c.Table.Path = path
	return c.Table
}

func (c *Creator) Add(name string, label string) *Field {
	item := new(Field)
	item.SetName(name).SetCnName(label).SetReName(name)
	c.Fields = append(c.Fields, item)
	return item
}

func (c *Creator) AddDocument(name string, cnName string, function func(subDoc *Creator) *Creator) *Creator {
	document := NewDocument(name, cnName, c.Table.Path)
	function(document)
	c.RelationTables = append(c.RelationTables, document)
	return c
}

func (c *Creator) Generate() {
	Init(c)
}
